//
//  BDHomeDetailPinglunTableViewCell.m
//  BadBoy
//
//  Created by wen on 2017/3/21.
//  Copyright © 2017年 com.8xiong. All rights reserved.
//

#import "BDHomeDetailPinglunTableViewCell.h"
#import "BDHomeDetailCommentsModel.h"
#import "BDTextLabel.h"
#import "LiuqsDecoder.h"
#import "BDHomeCommondLittleTableVeiwCell.h"

@interface BDHomeDetailPinglunTableViewCell ()<UITableViewDelegate, UITableViewDataSource>
{
    NSString *_comment;
}
@property (nonatomic, strong) BDTextLabel *contentLabel;
@property (nonatomic, strong) UIImageView *vipImageView;
@property (nonatomic, strong) BDHomeDetailCommentsModel *commentModel;

@property (nonatomic, strong) UIView *topLine;
@property (nonatomic, strong) UIView *bottomLine;

@property (nonatomic, strong) UIButton *replayButton;
//@property (nonatomic, strong) UIButton *zanButton;

@property (nonatomic, strong) UITapGestureRecognizer *tapGR;
@property (nonatomic, copy) void(^avatarClickBlock)();
@property (nonatomic, copy) void(^replayBlock)();
@property (nonatomic, copy) void(^replayUserClick)(NSInteger user_id);//点击头像的话只需要userid
@property (nonatomic, copy) void(^replayContrntClick)(NSString *userName, NSInteger user_id);//点击文字的话需要当前文字回复的userid和该评论的id

@property (nonatomic, strong) UITableView *tableView;

@end


@implementation BDHomeDetailPinglunTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    style = UITableViewCellStyleValue1;
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
    }
    return self;
}

- (void)configeWithModel:(id)model avatarClickBlock:(void(^)())avatarClickBlock replayBlock:(void(^)())replayBlock replayUserClick:(void(^)(NSInteger user_id))replayUserClick replayContrntClick:(void(^)(NSString *userName, NSInteger user_id))replayContrntClick{
    BDHomeDetailCommentsModel *commentModel = model;
    [self.imageView sd_setImageWithURL:[NSURL URLWithString:commentModel.user.cover] placeholderImage:[UIImage imageNamed:@"avatar"]];
    self.textLabel.text = commentModel.user.nickname;
    
    self.vipImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"showVip%ld",commentModel.user.vip_level]];
    
    NSDate *curDate = [NSDate dateWithTimeIntervalSince1970:commentModel.created_at.floatValue];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"MM-dd HH:mm"];
    NSString *dateStr = [formatter stringFromDate:curDate];
    self.detailTextLabel.text = dateStr;

    _comment = commentModel.comment;
    self.commentModel = commentModel;
    if (commentModel.user.vip_level != 0) {
        self.vipImageView.hidden = NO;
    }else{
        self.vipImageView.hidden = YES;
    }
    self.avatarClickBlock = avatarClickBlock;
    self.replayBlock = replayBlock;
    self.replayUserClick = replayUserClick;
    self.replayContrntClick = replayContrntClick;
    
    self.topLine.hidden = YES;
    if (self.isFirst == YES) {
        self.topLine.hidden = NO;
    }
    [self.tableView reloadData];
}

- (void)avatarTapAction{
    if (self.avatarClickBlock) {
        self.avatarClickBlock();
    }
}

- (void)layoutSubviews{
    self.imageView.frame = CGRectMake(10, 7, 40, 40);
    self.imageView.layer.masksToBounds = YES;
    self.imageView.layer.cornerRadius = 20;
    self.imageView.userInteractionEnabled = YES;
    
    if (self.tapGR == nil) {
        self.tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(avatarTapAction)];
        [self.imageView addGestureRecognizer:self.tapGR];
    }
    
    self.textLabel.textColor = [UIColor colorWithHexString:@"#1c1c1c"];
    self.textLabel.font = [UIFont systemFontOfSize:14];
    [self.textLabel sizeToFit];
    self.textLabel.top = 12;
    self.textLabel.left = self.imageView.right + 5;
    
    self.vipImageView.size = CGSizeMake(34 / 2, 25 / 2);
    self.vipImageView.centerY = self.textLabel.centerY;
    self.vipImageView.left = self.textLabel.right + 5;
    
    self.replayButton.size = CGSizeMake(20, 20);
    _replayButton.right = kBDScreenWidth - 10;
    _replayButton.centerY = self.vipImageView.centerY;
    
//    self.zanButton.size = CGSizeMake(20, 20);
//    _zanButton.right = self.replayButton.left - 15;
//    _zanButton.centerY = self.replayButton.centerY;
    
    self.detailTextLabel.font = [UIFont systemFontOfSize:10];
    self.detailTextLabel.textColor = [UIColor colorWithHexString:@"#767676"];
    self.detailTextLabel.size = CGSizeMake([self.detailTextLabel.text widthWithFont:self.detailTextLabel.font], 11);
    self.detailTextLabel.top = self.textLabel.bottom + 5;
    self.detailTextLabel.left = self.textLabel.left;
    
    
    self.contentLabel.numberOfLines = 0;
    self.contentLabel.textColor = [UIColor colorWithHexString:@"#1c1c1c"];
    self.contentLabel.width = self.width - 10 - self.textLabel.left;
    self.contentLabel.lineBreakMode = NSLineBreakByWordWrapping;
    self.contentLabel.left = self.textLabel.left;
    self.contentLabel.top = self.imageView.bottom + 8;
    self.contentLabel.textAlignment = NSTextAlignmentLeft;
//    NSMutableAttributedString *one = [LiuqsDecoder decodeWithPlainStr:_comment font:[UIFont systemFontOfSize:14]];
    NSMutableAttributedString *one = [[NSMutableAttributedString alloc] initWithString:_comment];
//    one.yy_lineSpacing = 3;
    one.font = [UIFont systemFontOfSize:14];
    
    self.contentLabel.attributedText = one;
    self.contentLabel.height = self.commentModel.contentHeight;
//    [self.contentLabel sizeToFit];

    self.tableView.width = kBDScreenWidth - 10 - self.contentLabel.left;
    self.tableView.height = self.commentModel.subCommentHeight;
    self.tableView.top = self.contentLabel.bottom + 10;
    self.tableView.left = self.contentLabel.left;
    
    self.topLine.backgroundColor = [UIColor colorWithHexString:@"#f2f2f2"];
    _topLine.size = CGSizeMake(kBDScreenWidth, 0.5);
    _topLine.top = 0;
    _topLine.left = 0;
    
    self.bottomLine.backgroundColor = [UIColor colorWithHexString:@"#f2f2f2"];
    _bottomLine.size = CGSizeMake(kBDScreenWidth, 0.5);
    _bottomLine.bottom = self.height;
    _bottomLine.left = 0;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.commentModel.sub_comments.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [self.commentModel.subCommentCellHeightArray[indexPath.row] floatValue];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    BDHomeCommondLittleTableVeiwCell *cell = [tableView dequeueReusableCellWithIdentifier:@"littleCommond"];
    if (cell == nil) {
        cell = [[BDHomeCommondLittleTableVeiwCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"littleCommond"];
    }
    
    BDHomeDetailCommentsModel *littleCommentModel = self.commentModel.sub_comments[indexPath.row];
    NSString *labelStr = nil;
    if (littleCommentModel.reply_user != nil) {
        labelStr = [NSString stringWithFormat:ASLocalizedString(@"%@：回复%@ %@"), littleCommentModel.user.nickname, littleCommentModel.reply_user.nickname, littleCommentModel.comment];
    }else{
        labelStr = [NSString stringWithFormat:@"%@：%@", littleCommentModel.user.nickname, littleCommentModel.comment];
    }
    NSMutableAttributedString *one = [[NSMutableAttributedString alloc] initWithString:labelStr];
    one.font = [UIFont systemFontOfSize:14];
    
    NSRange contentRange =[labelStr rangeOfString:labelStr];
    [one setTextHighlightRange:contentRange color:[UIColor colorWithHexString:@"#4c4c4c"] backgroundColor:nil userInfo:nil tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
        // BDLog(ASLocalizedString(@"点击回复的文字，表示要回复该回复"));
        if (self.replayContrntClick) {
            self.replayContrntClick(littleCommentModel.user.nickname, littleCommentModel.user.user_id);
        }
    } longPressAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
        // BDLog(ASLocalizedString(@"长按了回复的文字"));
    }];
    
    NSRange nameRange1 = [labelStr rangeOfString:littleCommentModel.user.nickname];
    [one setTextHighlightRange:nameRange1 color:KBDColorBlue backgroundColor:nil userInfo:nil tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
        // BDLog(ASLocalizedString(@"点击了自身的名字"));
        if (self.replayUserClick) {
            self.replayUserClick(littleCommentModel.user.user_id);
        }
    } longPressAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
        // BDLog(ASLocalizedString(@"长按了自身的名字"));
    }];
    
    if (littleCommentModel.reply_user != nil) {
        NSRange nameRange2 = [labelStr rangeOfString:littleCommentModel.reply_user.nickname];
        [one setTextHighlightRange:nameRange2 color:KBDColorBlue backgroundColor:nil userInfo:nil tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
            // BDLog(ASLocalizedString(@"点击了回复者的名字"));
            if (self.replayUserClick) {
                self.replayUserClick(littleCommentModel.reply_user.user_id);
            }
        } longPressAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
            // BDLog(ASLocalizedString(@"长按了回复者的名字"));
        }];
    }
    
    cell.text_Label.attributedText = one;
    cell.backgroundColor = [UIColor clearColor];
    return cell;
}

- (BDTextLabel *)contentLabel{
    if (!_contentLabel) {
        self.contentLabel = [[BDTextLabel alloc] init];
        [self addSubview:_contentLabel];
    }
    return _contentLabel;
}

- (UIImageView *)vipImageView{
    if (!_vipImageView) {
        self.vipImageView = [[UIImageView alloc] init];
        [self.contentView addSubview:_vipImageView];
    }
    return _vipImageView;
}

- (UIButton *)replayButton{
    if (!_replayButton) {
        self.replayButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_replayButton setImage:[UIImage imageNamed:@"pinglun"] forState:UIControlStateNormal];
        [self.contentView addSubview:_replayButton];
        [[_replayButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            if (self.replayBlock) {
                self.replayBlock();
            }
        }];
    }
    return _replayButton;
}

//- (UIButton *)zanButton{
//    if (!_zanButton) {
//        self.zanButton = [UIButton buttonWithType:UIButtonTypeCustom];
//        [_zanButton setImage:[UIImage imageNamed:@"zan"] forState:UIControlStateNormal];
//        [_zanButton setImage:[UIImage imageNamed:@"zan_sel"] forState:UIControlStateSelected];
//        [self.contentView addSubview:_zanButton];
//    }
//    return _zanButton;
//}

- (UIView *)topLine{
    if (!_topLine) {
        self.topLine = [[UIView alloc] init];
        [self.contentView addSubview:_topLine];
    }
    return _topLine;
}

- (UIView *)bottomLine{
    if (!_bottomLine) {
        self.bottomLine = [[UIView alloc] init];
        [self.contentView addSubview:_bottomLine];
    }
    return _bottomLine;
}

- (UITableView *)tableView{
    if (!_tableView) {
        self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = [UIColor colorWithHexString:@"#f2f2f2"];
        _tableView.scrollEnabled = NO;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self.contentView addSubview:_tableView];
    }
    return _tableView;
}
@end
